package com.pureut.quality.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pureut.common.core.utils.bean.BeanUtils;
import com.pureut.quality.domain.SysShopModeling;
import com.pureut.quality.domain.dto.ModelingMqDto;
import com.pureut.quality.domain.dto.ShopModelingDto;
import com.pureut.quality.mapper.SysShopModelingMapper;
import com.pureut.quality.service.ISysShopModelingService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/***
 *
 * Author:C
 * Date:2023/03/27 13:45
 * @DESC
 */
@Service
public class ISysShopModelingServiceImpl extends ServiceImpl<SysShopModelingMapper, SysShopModeling> implements ISysShopModelingService {

    @Resource
    SysShopModelingMapper sysShopModelingMapper;

    /**
     * 新增
     *
     * @param mq
     */
    @Override
    public void insertSysShopModeling(ModelingMqDto mq) {

        if (mq.getModelingCode() != null) {
            SysShopModeling sysShopModeling1 = sysShopModelingMapper.selectOne(new QueryWrapper<SysShopModeling>().lambda().eq(SysShopModeling::getModelingCode, mq.getModelingCode()));
            if(sysShopModeling1 == null){
                SysShopModeling sysShopModeling = new SysShopModeling();
                BeanUtils.copyProperties(mq, sysShopModeling);
                boolean save = save(sysShopModeling);
                System.out.println(save);
            }
        }

    }

    /**
     * 修改
     *
     * @param mq
     */
    @Override
    public void updateSysShopModeling(ModelingMqDto mq) {
        SysShopModeling sysShopModeling = sysShopModelingMapper.selectOne(new QueryWrapper<SysShopModeling>().lambda().eq(SysShopModeling::getModelingCode, mq.getModelingCode()));
        if (sysShopModeling != null) {
            BeanUtils.copyProperties(mq, sysShopModeling);
            int i = sysShopModelingMapper.updateById(sysShopModeling);
            System.out.println(i);
        }

    }

    /**
     * 删除
     *
     * @param mq
     */
    @Override
    public void deleteSysShopModeling(ModelingMqDto mq) {
        if (mq.getModelingCode() != null) {
            List<String> codes = Arrays.asList(mq.getModelingCode().split(","));
            List<SysShopModeling> sysShopModelings = sysShopModelingMapper.selectList(new QueryWrapper<SysShopModeling>().lambda().in(SysShopModeling::getModelingCode, codes));
            if (sysShopModelings.size() > 0) {
                removeBatchByIds(sysShopModelings);
            }
        }

    }

    /**
     * 根据阶别获取线别
     *
     * @param rank
     * @return
     */
    @Override
    public List<ShopModelingDto> getLineCodeByRank(int rank) {

        return sysShopModelingMapper.getLineCodeByRank(rank);
    }
}
